<template>
  <div class="accountList">
    <div class="topBtn">
      <el-button
        size="mini"
        @click="addDictDetailClick"
        type="success"
        plain
        icon="el-icon-plus"
        v-has="'system:dict:add'"
        >新增字典详情</el-button
      >
    </div>
    <div class="tableBox" ref="tableBoxRef">
      <el-table
        v-loading="tableLoading"
        :header-cell-style="{
          background: '#F8F8F8',
          color: '#333',
          fontSize: '16px',
          fontWeight: '700',
        }"
        :height="screenHeight - 300"
        border
        :data="tableData"
        style="width: 100%"
      >
        <el-table-column
          prop="label"
          align="center"
          label="字典标签"
           
        >
        </el-table-column>
        <el-table-column
          prop="value"
          align="center"
          label="字典键值"
           
        >
        </el-table-column>
         <el-table-column
          prop="order"
          align="center"
          label="字典排序"
           
        >
        </el-table-column>
        <el-table-column
          prop="remark"
          align="center"
          label="备注"
           
        >
        </el-table-column>
        <el-table-column
          prop="createTime"
          align="center"
          label="创建时间"
           
        >
          <template slot-scope="scope">{{
            scope.row.createTime | formatDate
          }}</template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              icon="el-icon-edit-outline"
              size="mini"
              type="text"
              v-has="'system:dict:edit'"
              @click="editClick(scope.row, scope.$index)"
              >编辑</el-button
            >
            <el-button
              icon="el-icon-delete"
              size="mini"
              type="text"
              @click="delClick(scope.row)"
              v-has="'system:dict:delete'"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
       <!-- 新增/修改权限 -->
    <el-dialog :close-on-click-modal="false" 
      :title="title"
      :visible.sync="addEditDialogVisible"
      width="500px"
      @open="addEditDialogOpen"
      class="addEditAccountDialog"
    >
      <div class="content">
        <el-form
          :model="addEditForm"
          :rules="addEditRules"
          ref="addEditFormRef"
        >
          <el-form-item prop="label" label="字典标签：">
            <div class="item">
              <el-input
                size="mini"
                v-model="addEditForm.label"
                placeholder="请输入字典名称"
                maxlength="16"
              >
              </el-input>
            </div>
          </el-form-item>
             <el-form-item prop="value" label="字典键值：">
            <div class="item">
              <el-input
                size="mini"
                v-model="addEditForm.value"
                placeholder="请输入字典键值"
                  maxlength="32"
              >
              </el-input>
            </div>
          </el-form-item>
           <el-form-item prop="order" label="字典排序：">
            <div class="item">
              <el-input
                size="mini"
                v-model="addEditForm.order"
                placeholder="请输入字典排序"
                  maxlength="32"
              >
              </el-input>
            </div>
          </el-form-item>
         <el-form-item prop="remark" label="备注：">
            <div class="item">
              <el-input
                type="textarea"
                size="mini"
                v-model="addEditForm.remark"
                placeholder="请输入备注"
                :rows="2"
              >
              </el-input>
            </div>
          </el-form-item>

          <el-form-item class="btns">
            <el-button size="mini" @click="addEditDialogVisible = false"
              >取 消</el-button
            >
            <el-button
              v-clickForbidden
              size="mini"
              type="primary"
              @click="addEditDialogSaveClick"
              >确 定</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      screenHeight: document.documentElement.clientHeight,
      tableLoading: false,
      tableData: [],
         addEditDialogVisible: false,
       addEditForm: {
       label:"",
       value:"",
       order:"",
       remark:""
      },
        addEditRules: {
        label: [
          { required: true, message: "字典标签不能为空", trigger: "blur" },
        ],
        value: [
          { required: true, message: "字典键值不能为空", trigger: "blur" },
        ],
        order: [
          { required: true, message: "字典排序不能为空", trigger: "blur" },
        ],
      },
         title: "新增字典详情",
      editId: "",
      dictId:'',
    };
  },
  methods: {
        /**
     * 新增字典
     * @param {*}
     */
    addDictDetailClick() {
      this.title = "新增字典详情";
      this.addEditDialogVisible = true;
    },
        /**
     * 打开新增和修改的回调
     * @param {*}
     */
    addEditDialogOpen() {
      if (this.$refs.addEditFormRef) this.$refs.addEditFormRef.resetFields();
    },
        /**
     * 获取标准库列表
     * @param {*}
     */
    async getDictDetail() {
      this.tableLoading = true;
      const res = await this.$api.getDictDetail(this.dictId);
      this.total = res.data.result.total;
      this.tableData = res.data.result;
      this.tableLoading = false;
    },
        /**
     * 编辑按钮
     * @param {*} row
     */
    async editClick(row,index) {
      this.title = "编辑字典详情";
      this.addEditDialogVisible = true;
      this.editId = row.id;
      this.$nextTick(() => {
        this.addEditForm = JSON.parse(JSON.stringify(this.tableData[index]))
      });
    },
        /**
     * 删除按钮
     * @param {*} row
     */
    delClick(row) {
      this.$confirm("确定删除当前字典详情吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
            console.log(row.id);
          this.$api.delDictDetail(row.id).then((res) => {
            if (res.data.code == "00000") {
              this.$message.success("删除成功");
              this.getDictDetail();
            }
          });
        })
        .catch(() => {});
    },
        /**
     * 新增编辑保存按钮
     * @param {*}
     */
    addEditDialogSaveClick() {
      this.$refs.addEditFormRef.validate(async (boolean) => {
        if (boolean) {
          let data = {
            dictId:this.dictId,
            id: this.editId,
           ...this.addEditForm
          };
          if (this.title == "新增字典详情") {
            data.id=''
            const res = await this.$api.addDictDetail(data);
            if (res.data.code == "00000") {
              this.$message.success("新增成功");
              this.getDictDetail();
              this.addEditDialogVisible = false;
            }
          } else {
            const res = await this.$api.editDictDetail(data);
            if (res.data.code == "00000") {
              this.$message.success("修改成功");
              this.getDictDetail();
              this.addEditDialogVisible = false;
            }
          }
        }
      });
    },
  },
  created() {
       this.dictId = this.$route.query.id;
       this.getDictDetail()
  },
};
</script>
<style  lang='less' scoped>
.accountList {
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: 20px;
   .topBtn {
    margin: 10px 0;
text-align: right;
  }
  .tableBox {
    flex: 1;
    .active {
      color: #409eff;
      cursor: pointer;
      text-decoration: underline;
    }
    .bottomBtn {
      height: 50px;
      display: flex;
      justify-content: flex-end;
      align-items: center;
    }
  }
      .btns {
      text-align: right;
      margin-bottom: 0;
    }
}
/deep/.el-button--success.is-plain {
  color: #fff;
  border-color: #409EFF;
  background-color: #409EFF;
}
</style>
